package com.study.studyelasticsearch.practice;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelUtil;
import com.study.studyelasticsearch.client.transportClient.TransportEsClient;
import com.study.studyelasticsearch.practice.bean.CompanyPosition;
import com.study.studyelasticsearch.practice.bean.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 吴宇亮 on 2022/5/18 14:53
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class InitPraticeData {

    private static final String fileDirPath;
    static {
        String classpath = InitPraticeData.class.getResource("/").toString();
        classpath = classpath
                .replace("file:/", "")
                .replace("/target/test-classes/", "/src/main/java/com/study/studyelasticsearch/practice/");
        fileDirPath = classpath;
    }

    private static final String companyPositionExcelFilePath = fileDirPath + "公司职位.xlsx";
    public static final String companyPositionIndexName = "study-company-position2";

    private static final String personExcelFilePath = fileDirPath + "姓名.xlsx";
    public static final String personIndexName = "study-person";

    @Autowired
    private TransportEsClient transportClient;

    /**
     * 初始化ES练习数据
     *
     * 1.先在招聘网站抓数据
     * 2.导入ES
     */
    @Test
    public void initPraticeEsData(){
        String[] indexs = new String[]{personIndexName, companyPositionIndexName};
        for (String index : indexs) {
            if (!transportClient.indexExist(index)) {
                transportClient.createIndex(index);
            }
        }

        List<Person> personList = this.getPersonDataFromFile();
        personList.forEach(item -> {
            transportClient.saveOrUpdateDoc(personIndexName, item);
        });

        List<CompanyPosition> companyPositionList = this.getCompanyPositionDataFromFile();
        companyPositionList.forEach(item -> {
            transportClient.saveOrUpdateDoc(companyPositionIndexName, item);
        });
    }

    private List<Person> getPersonDataFromFile() {
        List<Map<String, Object>> data = ExcelUtil.getReader(new File(personExcelFilePath)).readAll();
        return data.stream().map(dataLine -> {
            Person person = new Person();
            person.setName(dataLine.get("name").toString());
            return person;
        }).collect(Collectors.toList());
    }

    public List<CompanyPosition> getCompanyPositionDataFromFile(){
        List<Map<String, Object>> data = ExcelUtil.getReader(new File(companyPositionExcelFilePath)).readAll();
        return data.stream().map(dataLine -> {
            CompanyPosition companyPosition = new CompanyPosition();
            companyPosition.setCompanyId(dataLine.get("companyId").toString());
            companyPosition.setCity(dataLine.get("city").toString());
            companyPosition.setCompanyName(dataLine.get("companyFullName").toString());
            companyPosition.setPositionName(dataLine.get("positionName").toString());

            String salaryStr = dataLine.get("salary").toString();
            String[] salaryArr = salaryStr.split("-");
            companyPosition.setStartSalary(Integer.valueOf(salaryArr[0]));
            companyPosition.setEndSalary(Integer.valueOf(salaryArr[1]));
            return companyPosition;
        }).collect(Collectors.toList());
    }
}
